package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.handler.SimpleAsyncResponseHandler;
import com.foolishbird.sdk.params.BindPhoneResponseParams;
import com.foolishbird.sdk.params.FloatKefuResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.SendVerifyResponseParams;
import com.foolishbird.sdk.params.bean.FloatUserInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.params.bean.VerifyType;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.ui.assist.OnPhoneVerifyCodeListener;
import com.foolishbird.sdk.ui.assist.VerifyCodeCountDownTimer;
import com.foolishbird.sdk.ui.view.DialogFactory;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.ToolsUtils;


public class FloatPhoneChangeBindFragment extends BaseHttpFragment implements View.OnClickListener , OnPhoneVerifyCodeListener {


//    private LinearLayout phoneLayout,realnameLayout,versionLayout,pwdLayout;
    private TextView titleTxt;
    private ImageView backView;
    EditText phoneText,codeText;
    Button confirmBtn,codBtn;

    private VerifyCodeCountDownTimer mCountDownTimer;

    @Override
    public void onStop() {
        super.onStop();
        onCancelCountDownTimer();
    }


    @Override
    public void onStartCountDownTimer() {
        Button sendVerifyCodeBtn = getSendVerifyCodeView();
        if (sendVerifyCodeBtn.isClickable()) {
            sendVerifyCodeBtn.setClickable(false);
        }

        if (mCountDownTimer == null) {
            mCountDownTimer = new VerifyCodeCountDownTimer(sendVerifyCodeBtn);
        }
        mCountDownTimer.start();
    }

    @Override
    public void onCancelCountDownTimer() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }

        Button sendVerifyCodeBtn = getSendVerifyCodeView();
        if (!sendVerifyCodeBtn.isClickable()) {
            sendVerifyCodeBtn.setClickable(true);
            sendVerifyCodeBtn.setText(ResUtils.getStringIdentifier(getActivity(), "hnssn_get_captcha"));
        }
    }

    protected Button getSendVerifyCodeView() {
        return codBtn;
    }

    @Override
    public void onCallbackVerifyCode(String verifyCode) {

    }

    public static final FloatPhoneChangeBindFragment newInstance(Bundle bundle) {
        FloatPhoneChangeBindFragment fragment = new FloatPhoneChangeBindFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_float_changebind_phone");
        View contentView =inflater.inflate(resource,null);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("变更手机");
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        phoneText = contentView.findViewById(getViewIdentifier("fb_fragment_phone"));
        codeText = contentView.findViewById(getViewIdentifier("fb_fragment_login_code_edit"));
        confirmBtn = contentView.findViewById(getViewIdentifier("fb_fragment_login_login_btn"));
        codBtn = contentView.findViewById(getViewIdentifier("fb_fragment_login_code_send"));
        confirmBtn.setOnClickListener(this);
        codBtn.setOnClickListener(this);
        return contentView;

    }

    private FloatUserInfo userInfo;



    @Override
    public void onClick(View view) {
        if(view == backView){
//            if(userInfo!=null&&!TextUtils.isEmpty(userInfo.getId_number())){
//
//            }
            MyFragmentManger.getInstance().goBack(this);
        }else if(view == confirmBtn){
            String phoneNumber = phoneText.getText().toString().trim();
            String code = codeText.getText().toString().trim();
            if(TextUtils.isEmpty(phoneNumber)){
                Toast.makeText(getContext(),"请输入原绑定手机号",Toast.LENGTH_SHORT).show();
            }else if (TextUtils.isEmpty(code)){
                Toast.makeText(getContext(),"请输入验证码",Toast.LENGTH_SHORT).show();
            }else{
                Bundle bundle = new Bundle();
                bundle.putString("code",code);
                MyFragmentManger.getInstance().gotoFragment(FloatPhoneUnbindFragment.newInstance(bundle));
            }


        } else if (view ==codBtn) {
            String phoneNumber = phoneText.getText().toString().trim();

            if (TextUtils.isEmpty(phoneNumber)) {
                String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
                DialogFactory.createConfirmDialog(getActivity(), message).show();
                return;
            }

            if (!ToolsUtils.checkCellphone(phoneNumber)) {
                String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
                DialogFactory.createConfirmDialog(getActivity(), message).show();
                return;
            }

            SimpleAsyncResponseHandler handler = requestCallbackHandler(FloatKefuResponseParams.class);
            taskId =HttpEngineApi.getInstance().getFloatCode(phoneNumber, "verify", handler);

        }

//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }
    private int taskId ;
    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if (taskId == flag) {
                    onStartCountDownTimer();
                    Toast.makeText(getContext(),"获取验证码成功",Toast.LENGTH_SHORT).show();
                }else if (response instanceof BindPhoneResponseParams){
                    Toast.makeText(getContext(),"绑定成功",Toast.LENGTH_SHORT).show();
                    MyFragmentManger.getInstance().goBack(this);
                }
            }
        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
